package com.artPreter.service.impl;


import com.artPreter.mapper.ArtworkOtherPicMapper;
import com.artPreter.pojo.Article;
import com.artPreter.pojo.ArtworkOtherPic;
import com.artPreter.service.IArtworkOtherPicService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 艺术品额外图片表 服务实现类
 * </p>
 *
 * @author jarvis
 * @since 2025-01-14
 */
@Service
public class ArtworkOtherPicServiceImpl extends ServiceImpl<ArtworkOtherPicMapper, ArtworkOtherPic> implements IArtworkOtherPicService {

    @Override
    public List<ArtworkOtherPic> getList(ArtworkOtherPic artworkOtherPic) {
        List<ArtworkOtherPic> result = new ArrayList<>();
        if(artworkOtherPic.getArtworkId()!=null&&!artworkOtherPic.getArtworkId().equals("")){
            LambdaQueryWrapper<ArtworkOtherPic> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ArtworkOtherPic::getArtworkId,artworkOtherPic.getArtworkId());
            result = this.baseMapper.selectList(queryWrapper);
        }
        return result;
    }

    @Override
    public void saveSortInArtworkOtherPic(List<ArtworkOtherPic> artworkOtherPicList) {
        if(artworkOtherPicList!=null&&artworkOtherPicList.size()>0){
           LambdaQueryWrapper<ArtworkOtherPic> queryWrapper = new LambdaQueryWrapper<>();
           queryWrapper.eq(ArtworkOtherPic::getArtworkId,artworkOtherPicList.get(0).getArtworkId());
           List<ArtworkOtherPic> oldList = this.baseMapper.selectList(queryWrapper);
           if(artworkOtherPicList.get(0).getPicId()!=null&&artworkOtherPicList.get(0).getUrl()!=null&&artworkOtherPicList.get(0).getPicId().equals("000")&&artworkOtherPicList.get(0).getUrl().equals("000")&&artworkOtherPicList.size()==1){
               artworkOtherPicList = new ArrayList<>();
           }
            // 旧数据存在赋值1不进行操作，旧数据为0删除，新数据为0新增，新数据为1更新
            for (int i = 0; i < oldList.size(); i++) {
                for (int j = 0; j < artworkOtherPicList.size(); j++) {
                    if(oldList.get(i).getPicId().equals(artworkOtherPicList.get(j).getPicId())){
                        oldList.get(i).setIsExist(1);
                        artworkOtherPicList.get(j).setIsExist(1);
                    }
                }
            }
            // 旧数据为0删除
            for (int i = 0; i < oldList.size(); i++) {
                System.out.println(oldList.get(i)+"旧数据");
                if(oldList.get(i).getIsExist()==0){
                    this.baseMapper.deleteById(oldList.get(i));
                }
            }
            for (int i = 0; i < artworkOtherPicList.size(); i++) {
                if(artworkOtherPicList.get(i).getIsExist()==0){
                    String uuid = UUID.randomUUID().toString();
                    artworkOtherPicList.get(i).setPicId(uuid);
                    if(artworkOtherPicList.get(i).getName()==null){
                        artworkOtherPicList.get(i).setName(uuid);
                    }
                    this.baseMapper.insert(artworkOtherPicList.get(i));
                }else{
                    this.baseMapper.updateById(artworkOtherPicList.get(i));
                }
            }

        }
    }

}
